package com.river.boot.biz.service.impl;

import cn.hutool.core.lang.Assert;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.river.boot.biz.converter.ProductSpecOptionConverter;
import com.river.boot.biz.mapper.ProductSpecOptionMapper;
import com.river.boot.biz.model.entity.ProductSpecOption;
import com.river.boot.biz.model.form.ProductSpecOptionForm;
import com.river.boot.biz.model.query.ProductSpecOptionQuery;
import com.river.boot.biz.model.vo.ProductSpecOptionVO;
import com.river.boot.biz.service.ProductSpecOptionService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.List;

/**
 * 规格选项服务实现类
 *
 * @author JiangCheng Xiang
 * @since 2025-04-25 15:57
 */
@Service
@RequiredArgsConstructor
public class ProductSpecOptionServiceImpl extends ServiceImpl<ProductSpecOptionMapper, ProductSpecOption> implements ProductSpecOptionService {

    private final ProductSpecOptionConverter productSpecOptionConverter;

    /**
     * 获取规格选项分页列表
     *
     * @param queryParams 查询参数
     * @return {@link IPage<ProductSpecOptionVO>} 规格选项分页列表
     */
    @Override
    public IPage<ProductSpecOptionVO> getProductSpecOptionPage(ProductSpecOptionQuery queryParams) {
        Page<ProductSpecOptionVO> pageVO = this.baseMapper.getProductSpecOptionPage(
                new Page<>(queryParams.getPageNum(), queryParams.getPageSize()),
                queryParams
        );
        return pageVO;
    }

    /**
     * 获取规格选项表单数据
     *
     * @param id 规格选项ID
     * @return
     */
    @Override
    public ProductSpecOptionForm getProductSpecOptionFormData(Long id) {
        ProductSpecOption entity = this.getById(id);
        return productSpecOptionConverter.toForm(entity);
    }

    /**
     * 新增规格选项
     *
     * @param formData 规格选项表单对象
     * @return
     */
    @Override
    public boolean saveProductSpecOption(ProductSpecOptionForm formData) {
        ProductSpecOption entity = productSpecOptionConverter.toEntity(formData);
        return this.save(entity);
    }

    /**
     * 更新规格选项
     *
     * @param id       规格选项ID
     * @param formData 规格选项表单对象
     * @return
     */
    @Override
    public boolean updateProductSpecOption(Long id, ProductSpecOptionForm formData) {
        ProductSpecOption entity = productSpecOptionConverter.toEntity(formData);
        return this.updateById(entity);
    }

    /**
     * 删除规格选项
     *
     * @param ids 规格选项ID，多个以英文逗号(,)分割
     * @return
     */
    @Override
    public boolean deleteProductSpecOptions(String ids) {
        Assert.isTrue(StrUtil.isNotBlank(ids), "删除的规格选项数据为空");
        // 逻辑删除
        List<Long> idList = Arrays.stream(ids.split(","))
                .map(Long::parseLong)
                .toList();
        return this.removeByIds(idList);
    }

    @Override
    public List<ProductSpecOptionForm> getProductSpecOptionsByProductId(Long productId) {
        List<ProductSpecOption> productSpecOptions = this.baseMapper.selectProductSpecOptionsByProductId(productId);
        return productSpecOptionConverter.toFormList(productSpecOptions);
    }

    @Override
    public void deleteProductSpecOptionsByProductId(Long productId) {
        this.baseMapper.deleteProductSpecOptionsByProductId(productId);
    }

}
